<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Meter_reading_model extends CI_Model {
	public function __construct() {
		parent::__construct();
	}

public function get_status($num,$name,$from,$to,$z1,$z2){
	/*$where=" WHERE mrv.acct_num = '%' ";
	if(($num == "" && $name == "") && ($from != "" || $to != "")) {
		$where = "WHERE DATE_FORMAT(STR_TO_DATE(mrv.reading_date, '%d-%M-%y'),'%d-%M-%y') BETWEEN 
			IFNULL(
				DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%d-%M-%y') ,
				DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%d-%M-%y')) 
			AND 
			IFNULL(
				DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%d-%M-%y') ,
				DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%d-%M-%y'))";
	}else if(($from == '' && $to == '') && ($num != "" || $name != "")){ 
		$where = "WHERE mrv.acct_num = '".$num."' OR mrv.account_name = '".$name."' ";
	}
	else if($from != '' || $to != '' || $num != "" || $name != "" ){ 
		$where = "WHERE (mrv.acct_num = '".$num."' OR mrv.account_name = '".$name."') 
				 AND DATE_FORMAT(STR_TO_DATE(mrv.reading_date, '%d-%M-%y'),'%d-%M-%y') BETWEEN 
				 IFNULL(
				 	DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%d-%M-%y') ,
				 	DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%d-%M-%y'))
				 AND 
				 IFNULL(
				 	DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%d-%M-%y') ,
				 	DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%d-%M-%y'))";
	}
	
	$sql = "SELECT * FROM ubs_meter_reading_report_v mrv ".$where." ORDER BY DATE_FORMAT(STR_TO_DATE(mrv.reading_date, '%d-%M-%y'),'%d-%M-%y') DESC";

	$query = $this->db->query($sql);
	return $query->result();*/

	 	$sql = "";
        $sql1 = ""; 
        $sql2 = "";
        $sql3 = "";
        $sql4 = "";
        $sql5 = "";
        $arr = array();
        if(!empty($num)){
          $sql1 = " acct_num LIKE '". $num ."' ";
          array_push($arr,$sql1);
        }
        if(!empty($name)){
          $sql2 = " account_name LIKE '". $name ."' ";
          array_push($arr,$sql2);
        }
        if(!empty($from) || !empty($to)){
          $sql3 = " DATE_FORMAT(STR_TO_DATE(mrv.reading_date, '%d-%M-%y'),'%d-%M-%y') BETWEEN 
			IFNULL(
				DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%d-%M-%y') ,
				DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%d-%M-%y')) 
			AND 
			IFNULL(
				DATE_FORMAT(STR_TO_DATE('".$to."', '%d-%M-%y'),'%d-%M-%y') ,
				DATE_FORMAT(STR_TO_DATE('".$from."', '%d-%M-%y'),'%d-%M-%y')) ";
          array_push($arr,$sql3);
        }

        if(!empty($z1)){
          $sql4 = " Zone = '". $z1 ."' ";
          array_push($arr,$sql4);
        }

        if(!empty($z2)){
          $sql5 = " book = '". $z2 ."' ";
          array_push($arr,$sql5);
        }

        $where = "WHERE ".implode(' AND ',$arr);

        $sql = "SELECT * FROM ubs_meter_reading_report_v mrv ".$where." ORDER BY DATE_FORMAT(STR_TO_DATE(mrv.reading_date, '%d-%M-%y'),'%d-%M-%y') DESC";
        //echo $sql;
        $q = $this->db->query($sql);
        return $q->result();

}

	public function getzoning($zone) {
		$this->db->where('zone_code', $zone);
		$query = $this->db->get('ar_zones');
		return $query->result();
	}

	public function getzoning2($zone) {
		$this->db->where('zone_code', $zone);
		$query = $this->db->get('ar_zone_values');
		return $query->result();
	}

	public function getzoning3($zone) {
		$aa;
		$this->db->where('zone_value_name', $zone);
		$q1 = $this->db->get('ar_zone_values');
		foreach($q1->result() as $q){
			$aa = $q->zone_value_id;
		}
		$this->db->where('parent_value_id', $aa);
		$query = $this->db->get('ar_zone_values');
		return $query->result();
	}

}